﻿#ifndef MYOPENGLWIDGET_H
#define MYOPENGLWIDGET_H

#include <QKeyEvent>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
#include <QOpenGLWidget>

#include <QMouseEvent>
#include <QPoint>
#include <QTime>
#include <QTimer>
#include <QVector3D>
#include <QVector>
#include <QWheelEvent>
#include <math.h>

const float PI = 3.1415926;
/*
    相机自由移动，通过上下左右键：    view.lookAt( cameraPos, cameraPos + cameraFront, up );

    通过鼠标控制 ：
        cameraFront.setX( cos( yaw * PI / 180 ) * cos( pitch * PI / 180 ) );
        cameraFront.setY( sin( pitch * PI / 180 ) );
        cameraFront.setZ( sin( yaw * PI / 180 ) * cos( pitch * PI / 180 ) );

*/

class MyOpenGLWidget : public QOpenGLWidget, QOpenGLFunctions_3_3_Core {
    Q_OBJECT
  public:
    enum Shape { None, Rect, Circle, Triangle };

    MyOpenGLWidget( QWidget *parent = nullptr );
    ~MyOpenGLWidget();

    void drawShape( Shape shape );
    void setWireframe( bool wireframe );

  protected:
    virtual void initializeGL();
    virtual void resizeGL( int w, int h );
    virtual void paintGL();
    virtual void keyPressEvent( QKeyEvent *event ) override;
    virtual void mouseMoveEvent( QMouseEvent *event ) override;
    virtual void wheelEvent( QWheelEvent *event ) override;

  signals:

  public slots:
    void on_timerout();

  private:
    Shape                m_shape;
    QOpenGLShaderProgram m_shaderProgram;
    QOpenGLTexture *     m_textureWall;  // 1 步骤一
    QOpenGLTexture *     m_textureFreep; // 1 步骤一
    QOpenGLTexture *     m_textureSamll; // 1 步骤一

    QTimer m_timer;
    QTime  m_time;
    float  deltaTime;
    float  lastFram;
    QPoint deltaPos;
    QPoint lastPos;
    float  fov;

    QVector3D cameraPos;
    QVector3D cameraTarget;
    QVector3D cameraDirection;
    QVector3D up;
    QVector3D cameraRight;
    QVector3D cameraUp;
    QVector3D cameraFront;
};

#endif // MYOPENGLWIDGET_H
